package ph.edu.upd.eee.upcatarchivesearch;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by smartwire on 10/9/2014.
 */
public class AboutActivity extends Activity {
    ImageButton btnMoreInfo;
    TextView txtVerInfo, txtYearInfo, txtMoreInfo;
    SurfaceView sView;

    @Override
    protected void onCreate(Bundle savedStateInstance) {
        super.onCreate(savedStateInstance);
        setContentView(R.layout.activity_about);

        this.sView = (SurfaceView) findViewById(R.id.sview_about);
        this.txtVerInfo = (TextView) findViewById(R.id.txt_about_dbinfo);
        this.txtYearInfo = (TextView) findViewById(R.id.txt_about_years);
        this.txtMoreInfo = (TextView) findViewById(R.id.txt_about_more_info);
        this.btnMoreInfo = (ImageButton) findViewById(R.id.btn_about_more_info);

        txtMoreInfo.setVisibility(View.INVISIBLE);
        btnMoreInfo.setVisibility(View.INVISIBLE);
        btnMoreInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(txtMoreInfo.getVisibility() == View.VISIBLE) {
                    txtMoreInfo.setVisibility(View.INVISIBLE);
                }
                else {
                    txtMoreInfo.setVisibility(View.VISIBLE);
                }
            }
        });

        sView.getHolder().addCallback(new AboutSurfaceView(this));

        showUpdateFile();
    }

    private void showUpdateFile() {
        boolean other;
        int startYear, endYear, elmEndYear;
        String yearsAvailable, moreInfo;

        Resources resources;
        JSONObject jsonObject, lastUpdate, archiveInfo;
        JSONArray archiveArray;

        jsonObject = Utility.getUpdateStats(getExternalFilesDir(null) + Utility.ARCHIVE_DIR);
        resources = getResources();

        try {
            lastUpdate = jsonObject.getJSONObject("lastUpdate");
            archiveArray = jsonObject.getJSONArray("archives");

            other = false;
            yearsAvailable = "";
            moreInfo = resources.getString(R.string.txt_about_moreinfo) + "\n\n";
            archiveInfo = archiveArray.getJSONObject(0);
            startYear = endYear = archiveInfo.getInt("year");

            if(!archiveInfo.getString("incomplete").equals("")) {
                moreInfo += startYear + ": " + archiveInfo.getString("notes") + "\n";
            }

            for(int i = 1; i < archiveArray.length(); i++) {
                archiveInfo = archiveArray.getJSONObject(i);
                elmEndYear = archiveInfo.getInt("year");

                if(elmEndYear - endYear <= 1) {
                    endYear = elmEndYear;
                }

                if(elmEndYear - endYear > 1 || i >= archiveArray.length() - 1) {
                    // Print ranges
                    if(!other) {
                        other = true;
                    }
                    else {
                        yearsAvailable += ", ";
                    }

                    if (startYear == endYear) {
                        yearsAvailable += startYear;
                    } else {
                        yearsAvailable += startYear + "-" + endYear;
                    }

                    startYear = endYear = elmEndYear;
                }

                if(!archiveInfo.getString("incomplete").equals("")) {
                    moreInfo += elmEndYear + ": " + archiveInfo.getString("notes") + "\n";
                }
            }

            if(!moreInfo.equals("")) {
                txtMoreInfo.setText(moreInfo);

                btnMoreInfo.setVisibility(View.VISIBLE);
            }

            txtVerInfo.setText(resources.getString(R.string.txt_about_version) + " " + lastUpdate.getString("date") + " " + lastUpdate.getString("time"));
            txtYearInfo.setText(resources.getString(R.string.txt_about_years) + " " + yearsAvailable);
        }
        catch(Exception e) {
            // Do nothing...
            e.printStackTrace();
        }
    }
}
